<?php
/* 天启玩家类 */
class tq_user
{
    private $sql;
    /* sql初始化 */
    function init($db)  // 将sql对象传入
    {
        $this->sql = $db;
    }
    /* [ret str] 读取配置项 */
    function readSec($type, $file, $sec, $tem) // type:1临时,2系统 file:文件名称 sec:配置节 tem:配置项
    {
        $type = strToHex($type);
        $file = strToHex($file);
        $sec = strToHex($sec);
        $tem = strToHex($tem);
        $sql = "SELECT `内容` FROM `ini配置` WHERE `type` = '$type' AND `文件名称` = '$file' AND `配置节` = '$sec' AND `配置项` = '$tem'";
        $stmt = $this->sql->query($sql);
        $arr = $stmt->fetch();
        if ($arr == null) {
            return '';
        } else {
            $Row = $arr[0];
            return hexToStr($Row);
        }
    }
    /* [ret str] 写配置项 */
    function writeSec($type, $file, $sec, $tem, $cont) // type:1临时,2系统 file:文件名称 sec:配置节 tem:配置项
    {
        $type = strToHex($type);
        $file = strToHex($file);
        $sec = strToHex($sec);
        $tem = strToHex($tem);
        $cont = strToHex($cont);
        $sql = "SELECT `内容` FROM `ini配置` WHERE `type` = '$type' AND `文件名称` = '$file' AND `配置节` = '$sec' AND `配置项` = '$tem'";
        $stmt = $this->sql->query($sql);
        $arr = $stmt->fetch();
        if ($arr == null) {
            $sql = "INSERT INTO `ini配置` (`配置节`,`配置项`,`内容`,`type`,`文件名称`) VALUES (?,?,?,?,?)";
            $stmt = $this->sql->prepare($sql);
            $stmt->bindParam(1, $sec, PDO::PARAM_STR);
            $stmt->bindParam(2, $tem, PDO::PARAM_STR);
            $stmt->bindParam(3, $cont, PDO::PARAM_STR);
            $stmt->bindParam(4, $type, PDO::PARAM_STR);
            $stmt->bindParam(5, $file, PDO::PARAM_STR);
            return $stmt->execute();
        } else {
            $sql = "UPDATE `ini配置` SET `配置项` = ? WHERE `配置节`=? AND `内容`=? AND `type`=? AND `文件名称`=?";
            $stmt = $this->sql->prepare($sql);
            $stmt->bindParam(1, $tem, PDO::PARAM_STR);
            $stmt->bindParam(2, $sec, PDO::PARAM_STR);
            $stmt->bindParam(3, $cont, PDO::PARAM_STR);
            $stmt->bindParam(4, $type, PDO::PARAM_STR);
            $stmt->bindParam(5, $file, PDO::PARAM_STR);
            return $stmt->execute();
        }
    }
    /* [ret str] 获取玩家设置密码 */
    function password($obj) // obj：玩家QQ
    {
        $ret = $this->readSec(2, '系统数据', '玩家密码', $obj);
        if ($ret == '') {
            return $ret;
        } else {
            return md5(md5($ret));
        }
    }
    /* [ret str] 玩家登陆,返回cookies */
    function login($obj)
    {
        $temp = time() + mt_rand(100000, 999999);
        $this->writeSec(2, '系统数据', 'cookies', $temp, $obj);
        $this->writeSec(2, '系统数据', '玩家cookies', $obj, $temp);
        return $temp;
    }
    /* [ret bool] 判断登陆状态,返回布尔值 */
    function tLogin($obj, $cook)
    {
        // obj>QQ
        // cook>cookies
        $tObj = readSec('系统数据', 'cookies', $cook);
        $tCook = readSec('系统数据', '玩家cookies', $obj);
        if ($tObj != $obj || $tCook != $cook) {
            return false;
        } else {
            return true;
        }
    }
    /* [ret str] 获取玩家基础信息 */
    function user_info($obj, $info)
    {
        $sql = "SELECT `$info` FROM `玩家数据` WHERE QQ = ?";
        $stmt = $this->sql->prepare($sql);
        $stmt->bindParam(1, $obj, PDO::PARAM_INT, 12);
        if ($stmt->execute()) {
            $temp = $stmt->fetch();
            return $temp[0];
        } else {
            return '';
        }
    }
}
